<?php
class IndexController{
    private $data;
    public function __construct()
    {
        $this->data = include './data.php';
    }

    /**
     * 文章首页
     */
    public function index(){
        $data = $this->data;
        include './View/index.html';
    }
    /**
     * 发布文章
     */
    public function add(){
        if(IS_POST){
            //发布时间
            $_POST['sendtime'] = time();
            $this->data[] = $_POST;
            file_put_contents('./data.php',"<?php return " . var_export($this->data,true) . " ?>");
            success('添加成功','index.php');
        }
        include './View/add.html';
    }
//修改
    public function edit(){
		$id =intval($_GET['id']);
		$old = $this->data[$id];
		if($_POST){
			$_POST['sendtime']=$old['sendtime'];
			$this->data[$id] = $_POST;
			file_put_contents('./data.php',"<?php return " . var_export($this->data,true) . " ?>");
            success('修改成功','index.php');
		}
		include './View/edit.html';
    }
//删除
    public function del(){
		$id = intval($_GET['id']);
		unset($this->data[$id]);
		file_put_contents('./data.php',"<?php\r\nreturn ".var_export($this->data,true) . "?>");
		success('删除成功', 'index.php');
    }

//   阅读文件
    public function show(){
		$id = intval($_GET['id']);
		$od = $this->data[$id];
		include './View/show.html';
    }
}







